package scenario {
	
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;  
	
	import flash.display.*;
	import flash.text.TextField;
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import fl.controls.*;
	import flash.events.*;
	import mx.core.*;
	
	
	import com.google.maps.overlays.*;
	import com.google.maps.interfaces.*;
	import com.google.maps.LatLng;
	import com.google.maps.Map;
	import com.google.maps.MapEvent;
	import com.google.maps.MapType;
	import com.google.maps.controls.MapTypeControl;
	import com.google.maps.PaneId;
	import com.google.maps.LatLng;
	import com.google.maps.MapEvent;
	import com.google.maps.PaneId;
	import com.google.maps.interfaces.IMap;
	import com.google.maps.interfaces.IPane;
	import com.google.maps.overlays.OverlayBase;
	import com.google.maps.overlays.PolylineOptions;
	
	
	import scenario.Tag;
	
	public class ScenarioElement 
		extends OverlayBase
		implements IScenarioElement {
			
			protected var latlngs:Array;
			public var location:LatLng;
			protected var map:IMap;
			protected var sprite:Sprite;
			
			protected var type:String;
			protected var tag:String;
			protected var mode:String;

			
			public function ScenarioElement(tag:String, type:String) {
			
				this.tag = tag;
				this.type = type;
			}
			
			
			public function getElementType():String {
				return type;
			}
	
			/*
			public function ScenarioElement(ac:Array, option:PolylineOptions=null) {
			
				super();
				latlngs = ac;
				location = latlngs[0]
				
				sprite = new Sprite();
				
				this.addEventListener(MapEvent.OVERLAY_ADDED, onOverlayAdded);                          
				this.addEventListener(MapEvent.OVERLAY_REMOVED, onOverlayRemoved);
			}
			*/
	
			public function getTag():String {
				return tag;
			}
			public function getMode():String {
				return mode;
			}
			
			public override function getDefaultPane(map:IMap):IPane
			{
				this.map = map;
				return map.getPaneManager().getPaneById(PaneId.PANE_OVERLAYS);
			}
			
			public function getLatLngs(latlngs:Array):Array 
			{
				return latlngs;
			}
			
			public function getPoints(ac:Array):Array 
			{
				var points:Array = new Array(ac.length);
				for (var i:int=0; i<ac.length; i++) 
				{
					points[i] = sprite.globalToLocal(latlngToPoint(LatLng(ac[i])));
				}
				return points;
			}
			
			public function onOverlayAdded(event:MapEvent):void 
			{
				addChild(sprite);
				positionOverlay(true);
			}
			
			public function onOverlayRemoved(event:MapEvent):void {
				removeChild(sprite);
				sprite.graphics.clear();
			}
			
			public override function positionOverlay(zoomChanged:Boolean):void
			{
				if (zoomChanged) 
				{
					draw();
				}

				var myPoint:Point = latlngToPoint(location);
				sprite.x = myPoint.x;
				sprite.y = myPoint.y;
								
			}
			
			public function draw():void {
				throw new Error("Necessario implementare");
			}
			
			public function latlngToPoint(latlng:LatLng):Point 
			{
				return pane.fromLatLngToPaneCoords(latlng);
			}
			
			public function pointToLatLng(point:Point):LatLng 
			{
				return pane.fromPaneCoordsToLatLng(point);
			}
			
			public function changeMode(mode:String):void {
				throw new Error("Necessario implementare");
			
			}
			
	}
}